<template>
  <basic-container>
    <avue-crud ref="crud"
               :option="option"
               v-model:page="page"
               :table-loading="loading"
               @on-load="onLoad"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @refresh-change="refreshChange"
               @search-reset="searchChange"
               @search-change="searchChange"
               @selection-change="selectionChange"
               v-model="form"
               :data="data">
      <template #menu-left="{type,size}">
        <el-button type="danger"
                   :size="size" :disabled="multiple"
                   icon="el-icon-delete"
                   @click="handleBatchDelete">批量删除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script >
import {addEquipment, delEquipment, listEquipment, updateEquipment} from "/@/api/base/equipment";
import {reactive, ref, toRefs} from "vue";
import useCurrentInstance from "/@/utils/useCurrentInstance";
import {listEquipmentType} from "../../api/base/equipment";
import {listProcessInfo, processInfoFindById} from "../../api/base/processInfo";
export default {
  name: "equipment",

  setup(props){
    const { proxy } = useCurrentInstance();

    const option = ref({
      searchMenuSpan: 6,
      tip: false,
      border: true,
      index: false,
      viewBtn: false,
      selection: true,
      addBtn:true,
      searchLabelWidth:100,
      excelBtn:false,
      labelWidth:100,
      column: [


        {
          label: '设备编码',
          prop: "encode",
          minWidth: 150,search:true,
        },

        {
          label: '设备名称',
          prop: "name",
          minWidth: 150,search:true,

        },

        {
          label: '设备类型',
          prop: "type",
          minWidth: 150,search:true,
          type: 'select',
          allowCreate: true,
          filterable: true,
          dataType: "number",
          dicData:()=>{
            return listEquipmentType('').then(res=>{
             return res.data || []
            })
          }
        },

        {
          label: '序列号',
          prop: "serialNumber",
          minWidth: 150,
        },

        {
          label: '规格型号',
          prop: "specification",
          minWidth: 150,
        },
        {
          label: '关联工序编号',
          prop: "processEncode", display: false,
          minWidth: 150,
        },
        {
          label: '关联工序名称',
          prop: "processName", display: false,
          minWidth: 150,
        },

        {
          label: '关联工序',
          prop: "processId",
          minWidth: 150, hide:true,
          type: 'table',
          children: {
            border: true,searchMenuSpan: 6,
            column: [
              {
                label: '工序编码',
                prop: "encode", minWidth: 150, search: true
              },
              {
                label: '工序名称',
                prop: "name", minWidth: 150, search: true
              },

              {
                label: '工序性质',
                prop: "quality", minWidth: 150,
              },
            ],

          },
          formatter: (row) => {
            if (!row.name) return '';
            return row.name;
          },
          onLoad: ({page, value, data}, callback) => {
            console.log({page, value, data},"onLoad")
            if (value) {
              let id = value;
              if (Array.isArray(value) && value.length > 0) {
                id = value[0];
              }
              processInfoFindById({id: id}).then(res => {

                callback(res.data);
              })

              return;
            }
            listProcessInfo(Object.assign({page: page.currentPage, limit: page.pageSize,}, data))
                .then(res => {
                  const resData = res.data;
                  callback({
                    total: resData.total,
                    data: resData.list,
                  })
                })

          },
          props: {
            label: 'name',
            value: 'id'
          },
        },

        {
          label: '供应商',
          prop: "provider",search:true,
          minWidth: 150,
        },

        {
          label: '设备状态',
          prop: "status",
          type: 'select',
          dataType: "number",
          dicData:[
            {label:'离线',value:0},
            {label:'在线',value:1},
            {label:'故障',value:2},
          ],
          minWidth: 150,search:true,display: false,
        },

        {
          label: '申请人',
          prop: "applicantUserName",
          minWidth: 150,display: false,
        },


        {
          label: '审批人',
          prop: "approvalUserName",
          minWidth: 150, display: false,
        },



        {label: '录入/更新时间', prop: 'updateTime',display:false,
          search:true,searchRange:true, type: 'date',
          format: 'YYYY-MM-DD HH:mm:ss',
          searchLabelWidth: 120,minWidth: 180,
          valueFormat: 'YYYY-MM-DD HH:mm:ss',},

      ]
    });
    const loading = ref(true);
    const multiple  = ref(true);
    const data = ref([]);
    const crud = ref();
    const selectionList = ref([]);
    const queryParams = ref({});
    const form = ref({});
    const page = ref({
      pageSize: 10,
      currentPage: 1,
      total: 0

    })
    const onLoad = (pages, params = {}) => {
      loading.value = true;
      listEquipment(Object.assign({ page: pages.currentPage,
        limit: pages.pageSize,},params, queryParams.value)).then(res => {
        const resData =  res.data;
        page.value.total = resData.total;
        data.value = resData.list;
        loading.value = false;
        selectionClear();
      })
    }
    const selectionClear = () => {
      selectionList.value = [];
    }
    const rowSave = (row, done, loading) => {
      addEquipment(row).then(() => {
        done();
        onLoad(page.value);
        proxy.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    }
    const rowUpdate = (row, index, done, loading) => {
      updateEquipment(row).then(() => {
        done();
        onLoad(page.value);
        proxy.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    }
    const rowDel = (row) => {
      proxy.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
          .then(() => {
            return delEquipment({id:[row.id]});
          })
          .then(() => {
            onLoad(page.value);
            proxy.$message({
              type: "success",
              message: "操作成功!"
            });
          });
    }
    const refreshChange = () => {
      onLoad(page.value, queryParams.value);
    }
    const searchChange = (params, done) => {
      if (Array.isArray(params.updateTime) && params.updateTime.length > 0){
        params.beginTime = params.updateTime[0];
        params.endTime = params.updateTime[1];
      }
      page.currentPage = 1;
      onLoad(page.value, params);
      done();
    }
    const selectionChange = (list = []) => {
      selectionList.value = list;
      multiple.value = !(list.length > 0)

    }

    const handleBatchDelete = () => {
      let ids = selectionList.value.map(item => item.id);
      proxy.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
          .then(() => {
            return delEquipment({id:ids});
          })
          .then(() => {
            onLoad(page.value);
            proxy.$message({
              type: "success",
              message: "操作成功!"
            });
          });
    }


    return {
      option,
      loading,
      data,
      page,
      form,
      queryParams,
      multiple,
      onLoad,
      rowSave,
      rowUpdate,
      rowDel,
      refreshChange,
      searchChange,
      selectionChange,
      handleBatchDelete
    }
  }
}
</script>
